package com.ktg.mes.md.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ktg.mes.md.mapper.MesScalesOrderItemMapper;
import com.ktg.mes.md.domain.MesScalesOrderItem;
import com.ktg.mes.md.service.IMesScalesOrderItemService;



/**
 * 销售订单产品Service业务层处理
 *
 * @author byteco
 * @date 2022-09-01
 */
@Service
public class MesScalesOrderItemServiceImpl extends ServiceImpl<MesScalesOrderItemMapper, MesScalesOrderItem> implements IMesScalesOrderItemService
{
    @Autowired
    private MesScalesOrderItemMapper mesScalesOrderItemMapper;



    /**
     * 查询销售订单产品
     *
     * @param entity 销售订单产品
     * @param page 分页对象
     * @return 销售订单产品
     */
    @Override
    public IPage<MesScalesOrderItem> queryPageList(MesScalesOrderItem entity, Page<MesScalesOrderItem> page) {
        LambdaQueryWrapper<MesScalesOrderItem> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(MesScalesOrderItem::getCreateTime);
        IPage<MesScalesOrderItem> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询销售订单产品
     *
     * @param entity 销售订单产品
     * @return 销售订单产品
     */
    @Override
    public List<MesScalesOrderItem> queryList(MesScalesOrderItem entity) {
        LambdaQueryWrapper<MesScalesOrderItem> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(MesScalesOrderItem::getCreateTime);
        return this.list(lqw);
    }

    public MesScalesOrderItem getInfo(String id)
    {
        QueryWrapper<MesScalesOrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MesScalesOrderItem::getScalesOrderItemId, id);
        return this.getOne(queryWrapper);
    }


    /**
     * 新增销售订单产品
     *
     * @param entity 销售订单产品
     * @return 结果
     */
        @Transactional
    @Override
    public void insert(MesScalesOrderItem entity)
    {
        entity.setCreateTime(DateUtils.getNowDate());
        boolean sss = this.save(entity);
        log.error("true"+sss);
    }

    /**
     * 修改销售订单产品
     *
     * @param entity 销售订单产品
     * @return 结果
     */
        @Transactional
    @Override
    public boolean update(MesScalesOrderItem entity)
    {
        entity.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(entity);
    }

    /**
     * 批量删除销售订单产品
     *
     * @param scalesOrderItemIds 需要删除的销售订单产品主键
     * @return 结果
     */
    @Override
    public boolean delete(String[] scalesOrderItemIds)
    {
        return this.removeByIds(Arrays.asList(scalesOrderItemIds));
    }

    /**
     * 删除销售订单产品信息
     *
     * @param scalesOrderItemId 销售订单产品主键
     * @return 结果
     */
    @Override
    public boolean delete(String scalesOrderItemId)
    {
        return this.removeById(scalesOrderItemId);
    }

    private LambdaQueryWrapper<MesScalesOrderItem> buildQueryWrapper(MesScalesOrderItem entity) {
        LambdaQueryWrapper<MesScalesOrderItem> lqw = Wrappers.lambdaQuery();
                    lqw.like(StringUtils.isNotBlank(entity.getScalesOrderItemCode()), MesScalesOrderItem::getScalesOrderItemCode, entity.getScalesOrderItemCode());
                    lqw.eq(StringUtils.isNotBlank(entity.getOrderStatus()), MesScalesOrderItem::getOrderStatus, entity.getOrderStatus());
                    lqw.eq(StringUtils.isNotBlank(entity.getProductId()), MesScalesOrderItem::getProductId, entity.getProductId());
                    lqw.like(StringUtils.isNotBlank(entity.getProductCode()), MesScalesOrderItem::getProductCode, entity.getProductCode());
                    lqw.like(StringUtils.isNotBlank(entity.getProductName()), MesScalesOrderItem::getProductName, entity.getProductName());
                    lqw.eq(StringUtils.isNotBlank(entity.getProductSpc()), MesScalesOrderItem::getProductSpc, entity.getProductSpc());
                    lqw.eq(StringUtils.isNotBlank(entity.getUnitOfMeasure()), MesScalesOrderItem::getUnitOfMeasure, entity.getUnitOfMeasure());
                    lqw.eq(entity.getQuantity() != null, MesScalesOrderItem::getQuantity, entity.getQuantity());
                    lqw.eq(entity.getAmountMoney() != null, MesScalesOrderItem::getAmountMoney, entity.getAmountMoney());
                    lqw.eq(entity.getRequestDate() != null, MesScalesOrderItem::getRequestDate, entity.getRequestDate());
                    lqw.eq(StringUtils.isNotBlank(entity.getExplainDes()), MesScalesOrderItem::getExplainDes, entity.getExplainDes());
                    lqw.eq(StringUtils.isNotBlank(entity.getScalesOrderId()), MesScalesOrderItem::getScalesOrderId, entity.getScalesOrderId());
                    lqw.eq(StringUtils.isNotBlank(entity.getStatus()), MesScalesOrderItem::getStatus, entity.getStatus());
                    lqw.eq(StringUtils.isNotBlank(entity.getAttr1()), MesScalesOrderItem::getAttr1, entity.getAttr1());
                    lqw.eq(StringUtils.isNotBlank(entity.getAttr2()), MesScalesOrderItem::getAttr2, entity.getAttr2());
                    lqw.eq(entity.getAttr3() != null, MesScalesOrderItem::getAttr3, entity.getAttr3());
                    lqw.eq(entity.getAttr4() != null, MesScalesOrderItem::getAttr4, entity.getAttr4());
        lqw.eq(StringUtils.isNotBlank(entity.getClientId()), MesScalesOrderItem::getClientId, entity.getClientId());
        lqw.like(StringUtils.isNotBlank(entity.getClientCode()), MesScalesOrderItem::getClientCode, entity.getClientCode());
        lqw.like(StringUtils.isNotBlank(entity.getClientName()), MesScalesOrderItem::getClientName, entity.getClientName());
        return lqw;
    }

    /**
     * 排产，生产，完成数量统计
     * @param scalesOrderItemId
     * @param scheduling
     * @param product
     * @param complete
     */
    public void addCount(String  scalesOrderItemId,Double scheduling,Double product,Double complete){
        if (scalesOrderItemId == null){
            return;
        }
        MesScalesOrderItem mesScalesOrderItem = getById(scalesOrderItemId);
        if (mesScalesOrderItem == null){
            return;
        }
        double count = 0;
        if (mesScalesOrderItem.getQuantity() != null){
            count =  mesScalesOrderItem.getQuantity().doubleValue();
        }
        if (scheduling != null && scheduling != 0.0 ){
            double countScheduling = scheduling;
            if (mesScalesOrderItem.getCountScheduling() != null){
                countScheduling = mesScalesOrderItem.getCountScheduling().doubleValue()+scheduling;
            }
            double countSurplus = count - countScheduling;
            mesScalesOrderItem.setCountSurplus(BigDecimal.valueOf(countSurplus));
            mesScalesOrderItem.setCountScheduling(BigDecimal.valueOf(countScheduling));
        }
        if (product != null && product != 0.0 ){
            double countProduct = product;
            if (mesScalesOrderItem.getCountProduct() != null){
                countProduct = mesScalesOrderItem.getCountProduct().doubleValue()+product;
            }
            mesScalesOrderItem.setCountProduct(BigDecimal.valueOf(countProduct));
        }

        if (complete != null && complete != 0.0 ){
            double countComplete = complete;
            if (mesScalesOrderItem.getCountComplete() != null){
                countComplete = mesScalesOrderItem.getCountComplete().doubleValue()+complete;
            }
            mesScalesOrderItem.setCountComplete(BigDecimal.valueOf(countComplete));
        }
        updateById(mesScalesOrderItem);
    }



}
